package cn.gdut.chengcai.base;

public class IdentifyCodeUtil {

	/**
	 * 获取n位手机验证码
	 * 
	 * @param n
	 * @return
	 */
	public static String getPhoneIdentifyCode(int n) {

		// 手机验证码接口代码

		// n位验证码
		return IdentifyCodeUtil.getRandomNum(n);
	}

	/**
	 * 获取n位邮件验证码
	 * 
	 * @param n
	 * @return
	 */
	public static String getEmailIdentifyCode(int n) {

		// n位验证码
		return IdentifyCodeUtil.getRandomNum(n);
	}

	/**
	 * 获取n位随机数
	 * 
	 * @param n
	 * @return
	 */
	public static String getRandomNum(int n) {
		if (n < 0) {
			throw new IllegalArgumentException("随机数位数必须大于0");
		}
		String temp = String.valueOf(((long) (Math.random() * 9 * Math.pow(10, n - 1)) + (long) Math.pow(10, n - 1)));
		StringBuffer randomNum = new StringBuffer(temp);

		while (randomNum.length() < n) {
			randomNum.append(0);
		}
		return randomNum.toString();
	}
}
